package com.rnd.kx.mvp.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.rnd.kx.mvp.R;
import com.rnd.kx.mvp.bean.IndexBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class IndexCatesAdapter extends RecyclerView.Adapter<IndexCatesAdapter.ViewHolder> implements View
        .OnClickListener {
    private List<IndexBean.CatesBean> list;
    private Context context;

    public OnItemClickListener mOnItemClickListener;

    public IndexCatesAdapter(List<IndexBean.CatesBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public IndexCatesAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_index_cate, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(IndexCatesAdapter.ViewHolder holder, int position) {
        holder.itemView.setTag(position);
        final IndexRecipeAdapter indexRecipeAdapter = new IndexRecipeAdapter(list.get(position).getRecipe(), context);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        holder.recyclerView.setLayoutManager(linearLayoutManager);
        holder.recyclerView.setAdapter(indexRecipeAdapter);
        final IndexBean.CatesBean catesBean = list.get(position);
        if (catesBean.getTitle() != null && !TextUtils.isEmpty(catesBean.getTitle())){
            holder.title.setText(catesBean.getTitle());
        }

        indexRecipeAdapter.setOnItemClickListener(new IndexRecipeAdapter.OnItemClickListener() {
            @Override
            public void onClick(View view, IndexRecipeAdapter.ViewName viewName, int position) {
                switch (viewName){
                    case ITEM:

                        break;
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();
        if (mOnItemClickListener != null){
            switch (v.getId()){
                default:
                    mOnItemClickListener.onClick(v, ViewName.ITEM,position);
                    break;
            }
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.item_index_cate_recyclerview)
        RecyclerView recyclerView;
        @BindView(R.id.item_index_cate_title)
        TextView title;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
            itemView.setOnClickListener(IndexCatesAdapter.this);
        }
    }

    public interface OnItemClickListener {
        void onClick(View view, ViewName viewName, int position);
    }


    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.mOnItemClickListener=onItemClickListener;
    }

    public enum ViewName{
        ITEM

    }

}
